using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// Buff管理器 - 统一管理所有Buff的创建、更新和销毁
/// </summary>
public class BuffManager : MonoBehaviour
{
    private static BuffManager m_Instance;
    public static BuffManager Instance
    {
        get
        {
            if (m_Instance == null)
            {
                m_Instance = FindObjectOfType<BuffManager>();
                if (m_Instance == null)
                {
                    GameObject go = new GameObject("BuffManager");
                    m_Instance = go.AddComponent<BuffManager>();
                }
            }
            return m_Instance;
        }
    }

    /// <summary>
    /// 所有活跃的Buff
    /// </summary>
    private Dictionary<Entity, List<BuffBase>> m_ActiveBuffs = new Dictionary<Entity, List<BuffBase>>();

    /// <summary>
    /// Buff效果池
    /// </summary>
    private Dictionary<string, Queue<GameObject>> m_EffectPools = new Dictionary<string, Queue<GameObject>>();

    private void Awake()
    {
        if (m_Instance == null)
        {
            m_Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else if (m_Instance != this)
        {
            GameObject.Destroy(gameObject);
        }
    }

    /// <summary>
    /// 为实体添加Buff
    /// </summary>
    /// <param name="entity">目标实体</param>
    /// <param name="buffData">Buff数据</param>
    /// <param name="caster">施法者</param>
    public void AddBuffToEntity(Entity entity, BuffData buffData, Entity caster)
    {
        if (entity == null || buffData == null)
        {
            Logger.LogError("BuffManager", "参数不能为空");
            return;
        }

        // 检查实体是否有BuffComponent
        var buffComponent = entity.GetComponent<BuffComponent>();
        if (buffComponent == null)
        {
            Logger.LogError("BuffManager", $"实体 {entity.name} 没有BuffComponent");
            return;
        }

        // 使用BuffComponent添加Buff
        buffComponent.AddBuff(buffData, caster, entity);
    }

    /// <summary>
    /// 移除实体的指定Buff
    /// </summary>
    /// <param name="entity">目标实体</param>
    /// <param name="buffType">Buff类型</param>
    public void RemoveBuffFromEntity(Entity entity, BuffType buffType)
    {
        if (entity == null)
        {
            Logger.LogError("BuffManager", "实体不能为空");
            return;
        }

        var buffComponent = entity.GetComponent<BuffComponent>();
        if (buffComponent != null)
        {
            buffComponent.RemoveBuff(buffType);
        }
    }

    /// <summary>
    /// 清除实体的所有Buff
    /// </summary>
    /// <param name="entity">目标实体</param>
    public void ClearAllBuffsFromEntity(Entity entity)
    {
        if (entity == null)
        {
            Logger.LogError("BuffManager", "实体不能为空");
            return;
        }

        var buffComponent = entity.GetComponent<BuffComponent>();
        if (buffComponent != null)
        {
            // 这里需要在BuffComponent中添加清除所有Buff的方法
            // buffComponent.ClearAllBuffs();
        }
    }

    /// <summary>
    /// 获取Buff效果对象（使用对象池）
    /// </summary>
    /// <param name="effectPrefab">效果预制体</param>
    /// <returns>效果对象</returns>
    public GameObject GetEffect(GameObject effectPrefab)
    {
        if (effectPrefab == null) return null;

        string poolKey = effectPrefab.name;
        
        if (!m_EffectPools.ContainsKey(poolKey))
        {
            m_EffectPools[poolKey] = new Queue<GameObject>();
        }

        GameObject effect;
        if (m_EffectPools[poolKey].Count > 0)
        {
            effect = m_EffectPools[poolKey].Dequeue();
            effect.SetActive(true);
        }
        else
        {
            effect = GameObject.Instantiate(effectPrefab);
        }

        return effect;
    }

    /// <summary>
    /// 回收Buff效果对象
    /// </summary>
    /// <param name="effect">效果对象</param>
    public void ReturnEffect(GameObject effect)
    {
        if (effect == null) return;

        string poolKey = effect.name.Replace("(Clone)", "");
        
        if (!m_EffectPools.ContainsKey(poolKey))
        {
            m_EffectPools[poolKey] = new Queue<GameObject>();
        }

        effect.SetActive(false);
        m_EffectPools[poolKey].Enqueue(effect);
    }

    /// <summary>
    /// 检查实体是否有指定类型的Buff
    /// </summary>
    /// <param name="entity">目标实体</param>
    /// <param name="buffType">Buff类型</param>
    /// <returns>是否有该Buff</returns>
    public bool HasBuff(Entity entity, BuffType buffType)
    {
        if (entity == null) return false;

        var buffComponent = entity.GetComponent<BuffComponent>();
        if (buffComponent == null) return false;

        // 这里需要在BuffComponent中添加检查Buff的方法
        // return buffComponent.HasBuff(buffType);
        return false;
    }

    /// <summary>
    /// 获取实体的所有Buff
    /// </summary>
    /// <param name="entity">目标实体</param>
    /// <returns>Buff列表</returns>
    public List<BuffBase> GetEntityBuffs(Entity entity)
    {
        if (entity == null) return new List<BuffBase>();

        var buffComponent = entity.GetComponent<BuffComponent>();
        if (buffComponent == null) return new List<BuffBase>();

        // 这里需要在BuffComponent中添加获取所有Buff的方法
        // return buffComponent.GetAllBuffs();
        return new List<BuffBase>();
    }

    private void OnDestroy()
    {
        // 清理所有效果池
        foreach (var pool in m_EffectPools.Values)
        {
            while (pool.Count > 0)
            {
                var effect = pool.Dequeue();
                if (effect != null)
                {
                    GameObject.Destroy(effect);
                }
            }
        }
        m_EffectPools.Clear();
    }
}